import time
from selenium.webdriver.common.by import By
from common.Utils import ForumDriver

class ForumHome:
    url = ""
    driver=""
    def __init__(self):
        self.url="http://127.0.0.1:9580/index.html"
        self.driver=ForumDriver.driver
        # self.driver.get(self.url)
        # 添加隐式等待
        self.driver.implicitly_wait(3)
    #发布帖子测试
    def PostPostTest(self):
        #首先验证页面的关键信息是否都有
        #论坛图标、首页二字和发布帖子按钮

        self.driver.find_element(By.CSS_SELECTOR,"body > div.page > header.navbar.navbar-expand-md.navbar-light.d-print-none > div > h1 > a > img")
        self.driver.find_element(By.CSS_SELECTOR,"#bit-forum-content > div.page-header.d-print-none > div > div")
        self.driver.find_element(By.CSS_SELECTOR,"#bit-forum-content > div.page-header.d-print-none > div > div > div.col-auto.ms-auto.d-print-none > div > a.btn.btn-primary.d-none.d-sm-inline-block.article_post")
        #点击发布帖子按钮
        self.driver.find_element(By.CSS_SELECTOR,"#bit-forum-content > div.page-header.d-print-none > div > div > div.col-auto.ms-auto.d-print-none > div > a.btn.btn-primary.d-none.d-sm-inline-block.article_post").click()

        # 默认板块JAVA
        # 输入帖子标题
        self.driver.find_element(By.CSS_SELECTOR,"#article_post_title").send_keys("自动化测试")
        #输入内容
        self.driver.find_element(By.CSS_SELECTOR,"#edit-article > div.editormd-toolbar > div > ul > li:nth-child(7) > a > i").click()
        time.sleep(5)
        #点击发布按钮
        self.driver.find_element(By.CSS_SELECTOR,"#article_post_submit").click()
        # 预期结果发布成功，跳转论坛首页，并在最上方显示发布的帖子
        # 定位发布按钮验证，截图验证
        self.driver.find_element(By.CSS_SELECTOR,"#bit-forum-content > div.page-header.d-print-none > div > div > div.col-auto.ms-auto.d-print-none > div > a.btn.btn-primary.d-none.d-sm-inline-block.article_post")
        ForumDriver.getScreeShot()

    #查看帖子测试
    def ViewPostTest(self):
        # 首先验证页面的关键信息是否都有
        # 论坛图标、首页二字和发布帖子按钮
        self.driver.find_element(By.CSS_SELECTOR,"body > div.page > header.navbar.navbar-expand-md.navbar-light.d-print-none > div > h1 > a > img")
        self.driver.find_element(By.CSS_SELECTOR, "#bit-forum-content > div.page-header.d-print-none > div > div")
        self.driver.find_element(By.CSS_SELECTOR,"#bit-forum-content > div.page-header.d-print-none > div > div > div.col-auto.ms-auto.d-print-none > div > a.btn.btn-primary.d-none.d-sm-inline-block.article_post")
        # 验证论坛第一条帖子的用户头像、标题、用户名、发布时间、浏览量、点赞数和评论数是否存在
        self.driver.find_element(By.CSS_SELECTOR,"#artical-items-body > div:nth-child(1) > div > div.col-auto > span")
        self.driver.find_element(By.CSS_SELECTOR,"#artical-items-body > div:nth-child(1) > div > div.col > div.text-truncate")
        self.driver.find_element(By.CSS_SELECTOR,"#artical-items-body > div:nth-child(1) > div > div.col > div.text-muted.mt-2 > div > div.col > ul > li:nth-child(1)")
        self.driver.find_element(By.CSS_SELECTOR,"#artical-items-body > div:nth-child(1) > div > div.col > div.text-muted.mt-2 > div > div.col > ul > li:nth-child(2)")
        self.driver.find_element(By.CSS_SELECTOR,"#artical-items-body > div:nth-child(1) > div > div.col > div.text-muted.mt-2 > div > div.col-auto.d-none.d-md-inline > ul > li:nth-child(1)")
        self.driver.find_element(By.CSS_SELECTOR,"#artical-items-body > div:nth-child(1) > div > div.col > div.text-muted.mt-2 > div > div.col-auto.d-none.d-md-inline > ul > li:nth-child(2)")
        self.driver.find_element(By.CSS_SELECTOR,"#artical-items-body > div:nth-child(1) > div > div.col > div.text-muted.mt-2 > div > div.col-auto.d-none.d-md-inline > ul > li:nth-child(3)")
        #截图验证
        ForumDriver.getScreeShot()

        #点击第一条帖子查看帖子 ，进入帖子详情页
        self.driver.find_element(By.CSS_SELECTOR,"#artical-items-body > div:nth-child(1) > div > div.col > div.text-truncate > a > strong").click()
        time.sleep(3)
        # 验证用户名、用户头像、点赞和回复按钮是否存在
        self.driver.find_element(By.CSS_SELECTOR,"#article_details_author_name")
        self.driver.find_element(By.CSS_SELECTOR,"#article_details_author_avatar")
        self.driver.find_element(By.CSS_SELECTOR,"#details_btn_like_count")
        self.driver.find_element(By.CSS_SELECTOR,"#details_btn_article_reply")
        # 截图验证
        ForumDriver.getScreeShot()

    #点赞帖子测试
    def LikePostTest(self):
        # self.driver.find_element(By.CSS_SELECTOR,"#artical-items-body > div:nth-child(1) > div > div.col > div.text-truncate > a > strong").click()
        #获取帖子点赞数
        text = self.driver.find_element(By.CSS_SELECTOR,"#details_article_likeCount").text
        #点赞帖子
        self.driver.find_element(By.CSS_SELECTOR,"#details_btn_like_count").click()
        #获取点赞之后点赞数，验证是否更新
        text2 = self.driver.find_element(By.CSS_SELECTOR,"#details_article_likeCount").text
        assert text2==text
        #截图验证
        ForumDriver.getScreeShot()


    #回复帖子测试
    # def ReplyPostTest(self):
    #     #在回复框输入要回复的信息
    #     self.driver.find_element(By.CSS_SELECTOR, "#article_details_reply > div.CodeMirror.cm-s-default.CodeMirror-wrap.CodeMirror-empty > div.CodeMirror-scroll").send_keys("nice bro")
    #     #点击回复按钮
    #     self.driver.find_element(By.CSS_SELECTOR, "#details_btn_article_reply").click()
    #     #检查回复是否存在 且回复内容是否一致
    #     text = self.driver.find_element(By.CSS_SELECTOR, "#details_article_reply_content_349 > p").text
    #     assert text == "nice bro"
    #     # 截图验证
    #     ForumDriver.getScreeShot()

#创建home类对象
# home = ForumHome()
# #发布帖子测试
# home.PostPostTest()
#
# #查看帖子测试
# home.ViewPostTest()
#
# #点赞帖子测试
# home.LikePostTest()

